package com.rd.sys.common.utils.crypto;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.rd.sys.common.base.CommonConstants;

/**
 * 3DES工具类
 * 
 * @author
 * 
 */
public class TripleDES {
    private static final String Algorithm = "DESede"; // 定义 加密算法,可用
                                                      // DES,DESede,Blowfish

    /** 字符集 */
    private static final String Charset = CommonConstants.SysParamConstants.SYS_CHARSET;

    /**
     * 加密算法
     * 
     * @param key
     *            密钥
     * @param src
     *            待加密内容
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws Exception
     */
    private static byte[] encrypt(byte[] key, byte[] src) throws NoSuchAlgorithmException, NoSuchPaddingException,
            Exception {
        SecretKey deskey = new SecretKeySpec(key, Algorithm);
        Cipher cipher = Cipher.getInstance(Algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, deskey);
        return cipher.doFinal(src);
    }

    /**
     * 加密算法
     * 
     * @param key
     *            密钥
     * @param src
     *            待加密内容
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws Exception
     */
    public static byte[] encrypt(String key, String src) throws NoSuchAlgorithmException, NoSuchPaddingException,
            Exception {
        byte[] keyByte = key.getBytes(Charset);
        byte[] srcByte = src.getBytes(Charset);
        return encrypt(keyByte, srcByte);
    }

    /**
     * 加密算法
     * 
     * @param key
     *            密钥
     * @param src
     *            待加密内容
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws Exception
     */
    public static byte[] encrypt(String key, byte[] src) throws NoSuchAlgorithmException, NoSuchPaddingException,
            Exception {
        byte[] keyByte = key.getBytes(Charset);
        return encrypt(keyByte, src);
    }

    /**
     * 解密算法
     * 
     * @param key
     *            密钥
     * @param src
     *            待解密内容
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws Exception
     */
    private static byte[] decrypt(byte[] key, byte[] src) throws NoSuchAlgorithmException, NoSuchPaddingException,
            Exception {
        SecretKey deskey = new SecretKeySpec(key, Algorithm);
        Cipher cipher = Cipher.getInstance(Algorithm);
        cipher.init(Cipher.DECRYPT_MODE, deskey);
        return cipher.doFinal(src);

    }

    /**
     * 解密算法
     * 
     * @param key
     *            密钥
     * @param src
     *            待解密内容
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws Exception
     */
    public static byte[] decrypt(String key, byte[] src) throws NoSuchAlgorithmException, NoSuchPaddingException,
            Exception {
        byte[] keyByte = key.getBytes(Charset);
        return decrypt(keyByte, src);
    }

    /**
     * 解密算法(解密成字符串)
     * 
     * @param key
     *            密钥
     * @param src
     *            待解密内容
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws Exception
     */
    public static String decrypt2String(String key, byte[] src) throws NoSuchAlgorithmException,
            NoSuchPaddingException, Exception {
        byte[] destByte = decrypt(key, src);
        return new String(destByte, Charset);
    }
}
